package com.android.infantschool.ui.adapter;

import android.content.Context;
import android.nfc.Tag;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BabyClassPhotoBean;
import com.android.infantschool.bean.JobBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class BabyClassPhotoAdapter extends RecyclerView.Adapter<BabyClassPhotoAdapter.MyHolder> {

    private Context mContext;
    public List<BabyClassPhotoBean.DataBean> list = null;
    private OnLister lister;
    private int type = -1;//1:图片,2:视频
    private ClassPhotoAdapter classPhotoAdapter = null;

    public BabyClassPhotoAdapter(Context context, OnLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_baby_class_photo, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position1) {
        BabyClassPhotoBean.DataBean bean = list.get(position1);

        viewHolder.tvDate.setText(bean.getCreateTime());

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;//禁止滑动
            }
        };

        classPhotoAdapter = new ClassPhotoAdapter(mContext, new ClassPhotoAdapter.OnImgLister() {
            @Override
            public void imgLister(int position2) {
                if (lister != null) {
                    lister.lister(position1, position2, type);
                }
            }
        }, type);

        viewHolder.recyclerView.setLayoutManager(gridLayoutManager);
        viewHolder.recyclerView.setAdapter(classPhotoAdapter);
        classPhotoAdapter.setData(bean.getPhotoURL());
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    /**
     * 加载添加数据
     *
     * @param list
     */
    public void addDataAll(List<BabyClassPhotoBean.DataBean> list) {
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<BabyClassPhotoBean.DataBean> list, int type) {
        this.type = type;
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<BabyClassPhotoBean.DataBean> list) {
        this.list.addAll(0, list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public TextView tvDate;
        public RecyclerView recyclerView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tv_date);
            recyclerView = itemView.findViewById(R.id.recyclerView);
        }
    }

    public interface OnLister {
        void lister(int position1, int position2, int type);
    }
}